//
//  HelloWorldLayer.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/16/13.
//  Copyright Nguyen Tan Loc 2013. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
	if( (self=[super init]) )
    {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"circle.plist" textureFilename:@"circle.png"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"variousSprites@2x.plist" textureFilename:@"variousSprites@2x.png"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"gui_menu_sprite_3-ipadhd.plist" textureFilename:@"gui_menu_sprite_3-ipadhd.png"];


        bm = [[BulletManager alloc] init];
        [bm setPosition:ccp(512, 374)];
        [self addChild:bm];

        bi = [[BulletIndicator alloc] init];
        [bi setPosition:ccp(512, 374)];
//        [self addChild:bi];
        
        c = [[CircleManager alloc] init];
        [c setPosition:ccp(512, 374)];
        [self addChild:c];
        
        [c loadLevel:@"Party"];
        
        self.touchEnabled = true;
        [self scheduleUpdate];
    }
	return self;
}

-(void) update:(ccTime)delta
{
    [c checkCollideWithBullet:bm._arrActiveBullet];
//    CCLOG(@"%d --- %d", bm.children.count, c.children.count);
}

-(void) testButton:(CCMenuItem *)sender
{
    CCLOG(@"olaolaola");
//    [sender selected];
}

- (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//    CCLOG(@"touch");
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: [touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    [bi setStartPoint:touchLocation];
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: [touch view]];
    CGPoint prevLocation = [touch previousLocationInView: [touch view]];
    
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
    
    CGPoint diff = ccpSub(touchLocation, prevLocation);
    
    [bi setEndPoint:touchLocation];

        
//    [sprite setScale:sprite.scale + (diff.x / 5)];
    
//    float distance = ccpDistance(diff, ccp(512, 386));
//    [circle setRadius:circle._Radius + diff.x];
//    NSLog(@"Radius = %f", circle._Radius);
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
//    {
//        SLComposeViewController *fb = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//        [fb setInitialText:@"test facebook post on Project5"];
//        [[CCDirector sharedDirector] presentViewController:fb animated:YES completion:^{
//            CCLOG(@"tweet successed");
//        }];
//    }
//    else
//    {
//        UIAlertView *alert = [[UIAlertView alloc]
//                              initWithTitle:@"sorry"
//                              message:@"you suck"
//                              delegate:self
//                              cancelButtonTitle:@"OK"
//                              otherButtonTitles:nil];
//        [alert show];
//        [alert release];
//    }
//    [c testAppearAll];
    
    [bm fireCurrentBulletWithAngle:[bi getAngle]];
    [bi removeIndicator];
}

-(void) tweet
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweet setInitialText:@"test tweet in Project 5"];
        [tweet addImage:[UIImage imageNamed:@"Icon.png"]];
        [[CCDirector sharedDirector] presentViewController:tweet animated:YES completion:^{
            CCLOG(@"tweet successed");
        }];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"sorry"
                              message:@"you suck"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
